/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import Entity.HocPhanEntity;
import Util.ConSQL;
import Util.Util;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Thuyngaytho
 */
public class HocPhanDao {

    ConSQL cs = new ConSQL();
    Util util = new Util();
    //Them
    public ArrayList<HocPhanEntity> getAll() {
        HocPhanEntity hocPhanEntity;
        ArrayList<HocPhanEntity> list = new ArrayList<HocPhanEntity>();
        try {
            PreparedStatement ps = cs.getCon().prepareStatement("select * from lopHP");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                hocPhanEntity = new HocPhanEntity();
                hocPhanEntity.setMalopHP(rs.getString("maLopHP"));
                hocPhanEntity.setTenlopHP(rs.getString("tenLopHP"));
//                hocPhanEntity.setMakhoa(rs.getString("makhoa"));
                hocPhanEntity.setMakhoa(util.getTen("tenkhoa", "makhoa", rs.getString("makhoa"), "khoa"));
//                hocPhanEntity.setMaNK(rs.getString("maNK"));
                hocPhanEntity.setMaNK(util.getTen("tenNK", "maNK", rs.getString("maNK"), "nienKhoa"));
//                hocPhanEntity.setMaMH(rs.getString("maMH"));
                hocPhanEntity.setMaMH(util.getTen("tenMH", "maMH", rs.getString("maMH"), "monHoc"));
//                hocPhanEntity.setMaGV(rs.getString("maGV"));
                hocPhanEntity.setMaGV(util.getTen("tenGV", "maGV", rs.getString("maGV"), "giaoVien"));
                hocPhanEntity.setNgayBD(rs.getDate("ngayBatdau"));
                hocPhanEntity.setNgayKT(rs.getDate("ngayKetthuc"));
                hocPhanEntity.setSiso(rs.getInt("siSo"));
                hocPhanEntity.setSoSV(rs.getInt("soHSDK"));

                list.add(hocPhanEntity);
            }
        } catch (Exception e) {
        }
        return list;
    }

    public ArrayList<HocPhanEntity> timKiem(String str, int id) {
        HocPhanEntity hocPhanEntity;
        ArrayList<HocPhanEntity> list = new ArrayList<HocPhanEntity>();
        try {
            PreparedStatement ps;
            if (id == 0) {
                ps = cs.getCon().prepareStatement("select * from lopHP where maLopHP like N'%" + str + "%'");
            } else {
                ps = cs.getCon().prepareStatement("select * from lopHP where tenLopHP like N'%" + str + "%'");
            }
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                 hocPhanEntity = new HocPhanEntity();
                hocPhanEntity.setMalopHP(rs.getString("maLopHP"));
                hocPhanEntity.setTenlopHP(rs.getString("tenLopHP"));
//                hocPhanEntity.setMakhoa(rs.getString("makhoa"));
                hocPhanEntity.setMakhoa(util.getTen("tenkhoa", "makhoa", rs.getString("makhoa"), "khoa"));
//                hocPhanEntity.setMaNK(rs.getString("maNK"));
                hocPhanEntity.setMaNK(util.getTen("tenNK", "maNK", rs.getString("maNK"), "nienKhoa"));
//                hocPhanEntity.setMaMH(rs.getString("maMH"));
                hocPhanEntity.setMaMH(util.getTen("tenMH", "maMH", rs.getString("maMH"), "monHoc"));
//                hocPhanEntity.setMaGV(rs.getString("maGV"));
                hocPhanEntity.setMaGV(util.getTen("tenGV", "maGV", rs.getString("maGV"), "giaoVien"));
                hocPhanEntity.setNgayBD(rs.getDate("ngayBatdau"));
                hocPhanEntity.setNgayKT(rs.getDate("ngayKetthuc"));
                hocPhanEntity.setSiso(rs.getInt("siSo"));
                hocPhanEntity.setSoSV(rs.getInt("soHSDK"));

                list.add(hocPhanEntity);
            }
        } catch (Exception e) {
        }
        return list;
    }
}
